package cn.saas.chooseholeview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;


import java.util.List;

/**
 * @Title :
 * @date : 2021/6/18 11:51
 */
public class ChooseHoleView extends View {

    private Context mContext;
    private Paint paintX;
    private Paint paintY;
    private int screenWidth;

    private int defaultHeight = 60;
    private Path path;
    private float mRadius = 25;
    private Paint paintCircle;
    private int offset = 15; // 偏移量
    private Paint paintTxt;
    private int axPosition;
    private boolean isShow_A = false;
    private boolean isShow_B = false;
    private boolean isShow_C = false;
    private boolean isShow_D = false;
    private boolean isShow_O = false;
    private boolean isShow_Other = false;
    private Paint mPaint;

    //默认选中的钩子颜色
    int defaultCheckHookColor = Color.argb(255, 53, 40, 33);

    private float mHookLineLength = 250;
    private int index = 0;
    private int defaultSize = screenWidth;
    private onClick onClick;
    private Paint paintTxtStr;
    private boolean isShowTextStr = true;
    private boolean canTouch = true;
    private boolean canTouchA = true;
    private boolean canTouchB = true;
    private boolean canTouchC = true;
    private boolean canTouchD = true;
    private boolean canTouchO = true;
    private boolean canTouchOther = true;
    private float otherStrWidth;
    private List<Integer> mHoles;
    private Paint mHolePaint;

    public void setOnClick(ChooseHoleView.onClick onClick) {
        this.onClick = onClick;
    }

    public ChooseHoleView(Context context) {
        super(context);
    }

    public ChooseHoleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        screenWidth = ScreenUtils.getScreenWidth(mContext);
        initPaint();
    }

    private void initPaint() {
        paintX = new Paint();
        paintX.setAntiAlias(true);
        paintX.setColor(Color.BLACK);
        paintX.setStrokeWidth(3f);

        paintY = new Paint();
        paintY.setAntiAlias(true);
        paintY.setColor(Color.BLACK);
        paintY.setStrokeWidth(3f);


        path = new Path();
        path.moveTo(screenWidth / 2, 0);
        path.lineTo((screenWidth / 2 - defaultHeight / 2), defaultHeight);
        path.lineTo(screenWidth / 2 + defaultHeight / 2, defaultHeight);
        path.close();

        paintCircle = new Paint();
        paintCircle.setColor(Color.BLACK);
        paintCircle.setStyle(Paint.Style.STROKE);

        paintTxt = new Paint();
        paintTxt.setColor(Color.BLACK);
        paintTxt.setStyle(Paint.Style.STROKE);
        paintTxt.setTextSize(60);

        paintTxtStr = new Paint();
        paintTxtStr.setColor(Color.BLACK);
        paintTxtStr.setStyle(Paint.Style.STROKE);
        paintTxtStr.setTextSize(DimenUtils.sp2px(16));

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(defaultCheckHookColor);
        mPaint.setStrokeWidth(15);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mHolePaint = new Paint();
        mHolePaint.setAntiAlias(true);
        mHolePaint.setStyle(Paint.Style.FILL);
        mHolePaint.setColor(Color.GRAY);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMeasure = measureHandler(widthMeasureSpec);
        int heightMeasure = measureHandler(heightMeasureSpec);
        setMeasuredDimension(widthMeasure, heightMeasure);
    }

    private int measureHandler(int measureSpec) {
        int result = defaultSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = Math.min(defaultSize, specSize);
        } else {//相当于我们设置为match_parent
            result = screenWidth;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        axPosition = screenWidth / 4 + screenWidth / 8;
        if (false) {
            canvas.drawText("请选择钻孔", defaultHeight + offset, defaultHeight + offset, paintTxtStr);
        }

        String otherStr = "周边信息";
        otherStrWidth = paintTxtStr.measureText(otherStr);
        float otherX = screenWidth - offset - otherStrWidth - mRadius;
        drawHoles(canvas, otherX);

        canvas.drawText(isShow_A ? "✔" : "", axPosition - mRadius + offset, (screenWidth / 4 + defaultHeight) / 2 + offset, paintTxt);
        canvas.drawText(isShow_B ? "✔" : "", screenWidth / 2 + screenWidth / 8 - mRadius + offset, (screenWidth / 4 + defaultHeight) / 2, paintTxt);
        canvas.drawText(isShow_C ? "✔" : "", axPosition - mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2, paintTxt);
        canvas.drawText(isShow_D ? "✔" : "", screenWidth / 2 + screenWidth / 8 - mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2, paintTxt);
        canvas.drawText(isShow_O ? "✔" : "", screenWidth / 2 - mRadius / 2, screenWidth / 4 + defaultHeight + offset, paintTxt);
        canvas.drawText(isShow_Other ? "✔" : "", otherX - mRadius, screenWidth / 2 - defaultHeight + offset + mRadius, paintTxt);


        canvas.drawLine(screenWidth / 4, screenWidth / 4 + defaultHeight, screenWidth * 3 / 4, screenWidth / 4 + defaultHeight, paintX);
        canvas.drawLine(screenWidth / 2, defaultHeight, screenWidth / 2, screenWidth / 2 + defaultHeight, paintY);
        canvas.drawPath(path, paintX);

        canvas.drawCircle(screenWidth / 2, screenWidth / 4 + defaultHeight, mRadius, paintCircle);


        canvas.drawCircle(axPosition, (screenWidth / 4 + defaultHeight) / 2, mRadius, paintCircle);
        canvas.drawCircle(screenWidth / 2 + screenWidth / 8 - mRadius + offset, (screenWidth / 4 + defaultHeight) / 2, mRadius, paintCircle);
        canvas.drawCircle(axPosition, screenWidth / 4 + screenWidth / 8 + mRadius + offset, mRadius, paintCircle);
        canvas.drawCircle(screenWidth / 2 + screenWidth / 8 - mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset, mRadius, paintCircle);

        canvas.drawText("A", axPosition + mRadius + offset, (screenWidth / 4 + defaultHeight) / 2 + offset, paintTxt);
        canvas.drawText("B", screenWidth / 2 + screenWidth / 8 + mRadius + offset, (screenWidth / 4 + defaultHeight) / 2 + offset, paintTxt);
        canvas.drawText("C", axPosition + mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2, paintTxt);
        canvas.drawText("D", screenWidth / 2 + screenWidth / 8 + mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2, paintTxt);
        canvas.drawText("O", axPosition + screenWidth / 8 + mRadius + offset, (screenWidth / 4 + defaultHeight), paintTxt);

        canvas.drawText(otherStr, otherX, screenWidth / 2 - defaultHeight + mRadius * 2, paintTxtStr);
        canvas.drawCircle(otherX - mRadius, screenWidth / 2 - defaultHeight + offset + mRadius, mRadius, paintCircle);

    }


    public void setTextStrVisible(boolean isShowTextStr) {
        this.isShowTextStr = isShowTextStr;
    }

    public void setSelected(boolean canTouch, List<Integer> selected) {
        this.canTouch = canTouch;
        if (selected == null || selected.size() == 0) {
            return;
        }

        for (int i = 0; i < selected.size(); i++) {
            switch (selected.get(i)) {
                case 1:
                    isShow_A = true;
                    break;
                case 2:
                    isShow_B = true;
                    break;
                case 3:
                    isShow_C = true;
                    break;
                case 4:
                    isShow_D = true;
                    break;
                case 5:
                    isShow_O = true;
                    break;
                case 6:
                    isShow_Other = true;
                    break;
            }
        }
        invalidate();
    }

    public void setSelected(int selected) {
        this.canTouch = canTouch;
        if (selected == -1) {
            return;
        }
        switch (selected) {
            case 1:
                isShow_A = true;
                isShow_B = false;
                isShow_C = false;
                isShow_D = false;
                isShow_O = false;
                isShow_Other = false;
                break;
            case 2:
                isShow_B = true;
                isShow_A = false;
                isShow_C = false;
                isShow_D = false;
                isShow_O = false;
                isShow_Other = false;
                break;
            case 3:
                isShow_C = true;
                isShow_A = false;
                isShow_B = false;
                isShow_D = false;
                isShow_O = false;
                isShow_Other = false;
                break;
            case 4:
                isShow_D = true;
                isShow_A = false;
                isShow_B = false;
                isShow_C = false;
                isShow_O = false;
                isShow_Other = false;
                break;
            case 5:
                isShow_O = true;
                isShow_A = false;
                isShow_B = false;
                isShow_C = false;
                isShow_D = false;
                isShow_Other = false;
                break;
            case 6:
                isShow_Other = true;
                isShow_A = false;
                isShow_B = false;
                isShow_C = false;
                isShow_D = false;
                isShow_O = false;
                break;
        }
        invalidate();
    }


    /**
     * 设置选中
     *
     * @param noTouchPosition
     * @param selected
     */
    public void setSelected(List<Integer> noTouchPosition, List<Integer> selected) {
        if (selected == null || selected.size() == 0) {
            return;
        }

        for (int i = 0; i < selected.size(); i++) {
            switch (selected.get(i)) {
                case 1:
                    isShow_A = true;
                    break;
                case 2:
                    isShow_B = true;
                    break;
                case 3:
                    isShow_C = true;
                    break;
                case 4:
                    isShow_D = true;
                    break;
                case 5:
                    isShow_O = true;
                    break;
                case 6:
                    isShow_Other = true;
                    break;
                default:
                    break;
            }
        }

        for (int i = 0; i < noTouchPosition.size(); i++) {
            switch (noTouchPosition.get(i)) {
                case 1:
                    canTouchA = false;
                    break;
                case 2:
                    canTouchB = false;
                    break;
                case 3:
                    canTouchC = false;
                    break;
                case 4:
                    canTouchD = false;
                    break;
                case 5:
                    canTouchO = false;
                    break;
                case 6:
                    canTouchOther = false;
                    break;
            }
            switch (noTouchPosition.get(i)) {
                case 1:
                    canTouchA = false;
                    break;
                case 2:
                    canTouchB = false;
                    break;
                case 3:
                    canTouchC = false;
                    break;
                case 4:
                    canTouchD = false;
                    break;
                case 5:
                    canTouchO = false;
                    break;
                case 6:
                    canTouchOther = false;
                    break;
                default:
                    break;
            }
        }
        invalidate();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!canTouch) return true;
        float x = event.getX();
        float y = event.getY();
        PointF pointF = new PointF(x, y);
        PointF circleA = new PointF(axPosition + mRadius + offset, (screenWidth / 4 + defaultHeight) / 2 + offset);
        PointF circleB = new PointF(screenWidth / 2 + screenWidth / 8 - mRadius + mRadius + offset, (screenWidth / 4 + defaultHeight) / 2 + offset);
        PointF circleC = new PointF(axPosition + mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2);
        PointF circleD = new PointF(screenWidth / 2 + screenWidth / 8 - mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset * 2);
        PointF circleO = new PointF(screenWidth / 2 - mRadius - offset, screenWidth / 4 + mRadius);
        PointF circleOther = new PointF(screenWidth - otherStrWidth - defaultHeight - mRadius, screenWidth / 2 - defaultHeight);
        boolean pointInCircleA = isPointInCircle(pointF, circleA, 60);
        boolean pointInCircleB = isPointInCircle(pointF, circleB, 60);
        boolean pointInCircleC = isPointInCircle(pointF, circleC, 60);
        boolean pointInCircleD = isPointInCircle(pointF, circleD, 60);
        boolean pointInCircleO = isPointInCircle(pointF, circleO, 60);
        boolean pointInCircleOther = isPointInCircle(pointF, circleOther, 80);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (pointInCircleA) {
                    if (!canTouchA) return true;
                    index = 1;
                    invalidate();
                    isShow_A = !isShow_A;
                    isShow_B = false;
                    isShow_C = false;
                    isShow_D = false;
                    isShow_O = false;
                    isShow_Other = false;

                    if (onClick != null) {
                        onClick.onClick(isShow_A, "A");
                    }
                } else if (pointInCircleB) {
                    if (!canTouchB) return true;
                    index = 2;
                    invalidate();
                    isShow_B = !isShow_B;

                    isShow_A = false;
                    isShow_C = false;
                    isShow_D = false;
                    isShow_O = false;
                    isShow_Other = false;
                    if (onClick != null) {
                        onClick.onClick(isShow_B, "B");
                    }
                } else if (pointInCircleC) {
                    if (!canTouchC) return true;
                    index = 3;
                    invalidate();
                    isShow_C = !isShow_C;

                    isShow_A = false;
                    isShow_B = false;
                    isShow_D = false;
                    isShow_O = false;
                    isShow_Other = false;

                    if (onClick != null) {
                        onClick.onClick(isShow_C, "C");
                    }
                } else if (pointInCircleD) {
                    if (!canTouchD) return true;
                    index = 4;
                    invalidate();
                    isShow_D = !isShow_D;

                    isShow_A = false;
                    isShow_B = false;
                    isShow_C = false;
                    isShow_O = false;
                    isShow_Other = false;
                    if (onClick != null) {
                        onClick.onClick(isShow_D, "D");
                    }
                } else if (pointInCircleO) {
                    if (!canTouchO) return true;
                    index = 5;
                    invalidate();
                    isShow_O = !isShow_O;
                    isShow_A = false;
                    isShow_B = false;
                    isShow_D = false;
                    isShow_C = false;
                    isShow_Other = false;
                    if (onClick != null) {
                        onClick.onClick(isShow_O, "O");
                    }
                } else if (pointInCircleOther) {
                    if (!canTouchOther) return true;
                    index = 6;
                    invalidate();
                    isShow_Other = !isShow_Other;
                    isShow_A = false;
                    isShow_B = false;
                    isShow_C = false;
                    isShow_D = false;
                    isShow_O = false;

                    if (onClick != null) {
                        onClick.onClick(isShow_Other, "其他");
                    }
                }
                break;
            default:
                break;
        }
        return true;
    }


    private void drawHoles(Canvas canvas, float otherX) {
        // 画圆
        for (int i = 0; i < mHoles.size(); i++) {
            Integer integer = mHoles.get(i);
            switch (integer) {
                case 1:
                    canvas.drawCircle(axPosition, (screenWidth / 4 + defaultHeight) / 2, mRadius, mHolePaint);
                    canTouchA = false;
                    break;
                case 2:
                    canvas.drawCircle(screenWidth / 2 + screenWidth / 8 - mRadius + offset, (screenWidth / 4 + defaultHeight) / 2, mRadius, mHolePaint);
                    canTouchB = false;
                    break;
                case 3:
                    canvas.drawCircle(axPosition, screenWidth / 4 + screenWidth / 8 + mRadius + offset, mRadius, mHolePaint);
                    canTouchC = false;
                    break;
                case 4:
                    canvas.drawCircle(screenWidth / 2 + screenWidth / 8 - mRadius + offset, screenWidth / 4 + screenWidth / 8 + mRadius + offset, mRadius, mHolePaint);
                    canTouchD= false;
                    break;
                case 5: // O
                    canvas.drawCircle(screenWidth / 2, screenWidth / 4 + defaultHeight, mRadius, mHolePaint);
                    canTouchO = false;
                    break;
                case 6:
                    canvas.drawCircle(otherX - mRadius, screenWidth / 2 - defaultHeight + offset + mRadius, mRadius, mHolePaint);
                    break;
            }
        }
    }

    /**
     * 判断点是否在圆内
     *
     * @param pointF 待确定点
     * @param circle 圆心
     * @param radius 半径
     * @return true在圆内
     */
    private boolean isPointInCircle(PointF pointF, PointF circle, float radius) {
        return Math.pow((pointF.x - circle.x), 2) + Math.pow((pointF.y - circle.y), 2) <= Math.pow(radius, 2);
    }

    public void setHoles(List<Integer> integers) {
        this.mHoles = integers;
        invalidate();
    }

    public interface onClick {
        void onClick(boolean isShow, String hole);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
}
